<!DOCTYPE html>
<!--

// Copyright 2011 Traceur Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

-->
<html>
<head>
<title></title>
<script src="../../../third_party/closure-library/closure/goog/base.js"></script>
<script>

goog.require('goog.testing.jsunit');
goog.require('goog.testing.MockControl');

</script>
<script src="../../../bin/traceur.js"></script>
</head>
<body>
<script>

var mocks = new goog.testing.MockControl();
var mockError, mockWarn;

function setUp() {
  mockError = mocks.createMethodMock(console, 'error');
  mockWarn = mocks.createMethodMock(console, 'warn');
}

function tearDown() {
  mocks.$tearDown();
}

function testErrorReporter() {
  var r = new traceur.util.ErrorReporter();

  mockError('a%sc%se', 'b', 'd');
  mockWarn('a%sc%se', 'b', 'd');

  mocks.$replayAll();

  r.reportError(null, 'a%sc%se', 'b', 'd');
  r.reportWarning(null, 'a%sc%se', 'b', 'd');

  mocks.$verifyAll();
}

function testErrorReporterWithLocation() {
  var r = new traceur.util.ErrorReporter();
  var file = new traceur.syntax.SourceFile('test.js', '');
  var location = new traceur.util.SourcePosition(file, 1);
  location.line_ = 2;
  location.column_ = 3;

  mockError('test.js:3:4: a%sc%se', 'b', 'd');
  mockWarn('test.js:3:4: a%sc%se', 'b', 'd');

  mocks.$replayAll();

  r.reportError(location, 'a%sc%se', 'b', 'd');
  r.reportWarning(location, 'a%sc%se', 'b', 'd');

  mocks.$verifyAll();
}

function testMutedErrorReporter() {
  var r = new traceur.util.MutedErrorReporter();

  mocks.$replayAll();

  r.reportError(null, 'a%sc%se', 'b', 'd');
  r.reportWarning(null, 'a%sc%se', 'b', 'd');

  mocks.$verifyAll();
}

function testFormat() {
  var  format = traceur.util.ErrorReporter.format;
  assertEquals('loc: msg', format('loc', 'msg'));
  assertEquals('msg', format(null, 'msg'));

  assertEquals('1 + 2 = 3', format(null, '%s + %s = %s', [1, 2, 3]));
  assertEquals('a % b', format(null, 'a % b'));
  assertEquals('a % b', format(null, 'a %% b'));
  assertEquals('%', format(null, '%%'));
  assertEquals('%%', format(null, '%%%'));
  assertEquals('%%', format(null, '%%%%'));
  assertEquals('%%%', format(null, '%%%%%'));

  assertEquals('undefined', format(null, '%s'));
}

function testDevtoolsLink() {
  var file = new traceur.syntax.SourceFile('error_reporter_test.html', '');
  var location = new traceur.util.SourcePosition(file, 1);
  location.line_ = 108;
  location.column_ = 3;

  var segments = window.location.href.split('/');
  segments.pop();
  segments.push(location.toString());
  var linkable = segments.join('/');
  console.log('click on this link in devtools console: ' + linkable + '  ');
}

</script>
</body>
</html>
